<template id="new_address">
	<div class="new_address">

		<!--头部-->
		<div class="about_header">

			<div class="about_title">
				<router-link to="/select_address"><img src="../../assets/images/class/jiantou_03.png" /></router-link>

				<p>添加地址</p>
				<div class="about_select">
					<div class="about_ing"></div>
				</div>

			</div>
		</div>
		<!--头部结束-->

		<!--修改个人信息-->
		<div class="edit_means">

			<div class="select_date">
				<div class="select_ipt">
					<img src="../../assets/images/new_address/new_07.png" />
					<input type="text" name="" id="" placeholder="约课人" maxlength="10" value="user.id" v-model="username" @change="nameChange($event)" />
					<img src="../../assets/images/new_address/new_22.png" @click.stop="nameMost" />

				</div>
			</div>

			<div class="select_date">
				<div class="select_ipt">
					<img src="../../assets/images/new_address/new_11.png" />
					<input type="number" name="" id="" placeholder="请输入您的联系方式" oninput="if(value.length>6)value=value.slice(0,15)"  v-model="phone" @change="phhotoChange($event)" />
					<img src="../../assets/images/new_address/new_22.png" @click.stop="phoneMost" />

				</div>
			</div>

			<div class="select_data">
				<div class="select_ipt">
					<img src="../../assets/images/new_address/new_15.png" />
					<router-link :to="{path: '/search_address_two',query: {
							'username':this.username,
							'phone':this.phone,
							'detailAddr':this.detailAddr,
							'about_coach':this.$route.query.about_coach,
							'coachid': this.$route.query.coachid
						}}" style="width:100%;display: flex;"><span style="display: flex;margin-left: 10px;width:100%;border: none;
    background: none;
    outline: none;
    font-size: 18px;color:#757575">{{address_detail}}</span>
						<!-- <input type="text" name="" 
						id="" v-model="city" style="margin-left:0;" @change="cityChange($event)"/> -->
				</router-link>
				</div>
			</div>
			<div class="select_date">
				<div class="select_ipt">
					<img src="../../assets/images/new_address/new_19.png" />
					<input type="text" name="" id="" placeholder="请输入街道(楼栋及门牌号)" maxlength="30" v-model="detailAddr" @change="detailAddrChange($event)"/>
					<img src="../../assets/images/new_address/new_22.png" @click.stop="detailAddrMost"/>

				</div>
			</div>

			<!--<div class="select_datk">
				<div class="select_ipt">
					<img src="../../assets/images/new_address/new_27.png" />
					<span>家</span>
					<span>公司</span>
					<span>其他</span>

				</div>
			</div>-->

		</div>

		<!--修改个人信息结束-->

		<!--用户协议及隐私政策-->
		<div class="edit_policy">
			<img src="../../assets/images/new_address/duigou.png" />
			<span>用户协议及隐私政策</span>
		</div>
		<!--用户协议及隐私政策结束-->

		<!--提交修改-->
		<div class="edit_submit">
			<button @click.stop="joinKeep">保存</button>
		</div>
		<!--提交修改结束-->

	</div>
</template>

<script>
	export default {
		data() {
			return {
				index: '0',
				phone: this.$route.query.phone,
				username: this.$route.query.username,
				city:'',				
				area:'',
				street:'',
				edifice:'',
				detailAddr:this.$route.query.detailAddr,
				address_detail:'',
				jd:'',
				wd:''
				

			}
		},
		created() {
		// this.address_detail = decodeURI(this.$route.query.baiduapi);
		 var baidu=decodeURI(this.$route.query.baiduapi)
       if (baidu == 'undefined') {
        	this.address_detail='请输入详细地址'
		}else if(baidu == ''){
			this.address_detail='请输入详细地址'
		}else{
			this.address_detail=baidu
	   }
	   
       console.log(baidu)
        this.jd = decodeURI(this.$route.query.jd);
        this.wd = decodeURI(this.$route.query.wd);
        console.log(this.wd,this.jd)
        console.log(this.address_detail)
        let adddz=this.address_detail
         this.city=adddz.substring(0,adddz.indexOf('市')+1)
         this.area=adddz.substring(3,adddz.indexOf('区')+1)
         this.street=adddz.substring(6)
        console.log()
        console.log(this.street)
		},
		mounted(){
			
		},
		methods: {
			HeightL: function(index) {
				this.index = index
			},
			nameMost() {
				this.username = "";
			},
			nameChange(e) {
				this.username = e.target.value;
				console.log(e.target.value)
			},
			phoneMost() {
				this.phone = "";
			},
			phhotoChange(e) {
				this.phone = e.target.value;
			},
			cityChange(e) {
				this.city = e.target.value;
				// this.$router.push({
				// 		path: "/search_address",
						// query: {
						// 	'username':this.username,
						// 	'phone':this.phone,
						// 	'detailAddr':this.detailAddr
						// }
				// 	})
			},
			detailAddrChange(e){
				this.detailAddr = e.target.value;
			},
			detailAddrMost(){
				this.detailAddr = "";
			},
			joinKeep() {
				//let SESSION_ID = JSON.parse(window.sessionStorage.getItem('SESSION_ID'));
				 var SESSION_ID=JSON.parse(window.localStorage.getItem('sessiond'))
				console.log(SESSION_ID.user.id)
				let _this = this
			
					let username = _this.username; //姓名
					let phone = _this.phone; //手机号
//					let city = _this.city;
//					let area = _this.area;
//					let street = _this.street;
					let edifice = _this.edifice;
					let detailAddr = _this.detailAddr

					let obj = {
						//请求的参数
						//coachid: 88891,
						code: 1,
						userid: SESSION_ID.user.id,
						username:this.username,
						city:this.city,
						phone:this.phone,
						area:this.area,
						street:this.street,
						edifice:this.detailAddr,
						detailAddr:this.detailAddr,
						lng:this.jd,
						lat:this.wd,
					};
					if(_this.phone == "") {

					} else {

					}

					let qs = require('qs');
					this.$axios.post("/yujia/user/addAddressByUser", qs.stringify(obj), {
						headers: {
							'Content-Type': 'application/x-www-form-urlencoded'
						}
						//重写headers 请求头  qs.stringify(json对象) 把对象转化为字符串
					}).then((response) => {
						console.log(response.data.data.city);
						let vail = response.data.data.city;
				         _this.city = vail.split("|");
				         console.log(_this.city)
//				         console.log(_this.city)
						if(response.data.code ==0){
							mui.alert("信息填写不完整,请重新添加")
						}else if(response.data.code ==1){
							mui.alert("添加成功")
              var sessionid = window.localStorage.getItem("sessiond");
							var allsession = JSON.parse(sessionid);
							allsession.defaultaddress = response.data.data;
							window.localStorage.setItem("sessiond",JSON.stringify(allsession));
              if(this.$route.query.about_coach!=null){
                let session = JSON.parse(window.localStorage.getItem('sessiond'));
                console.log(session);
                session.defaultaddress = response.data.data;
                console.log(session);
                window.localStorage.setItem("sessiond",JSON.stringify(session));
                window.localStorage.setItem("add",JSON.stringify(response.data.data.city+response.data.data.area+response.data.data.street));
                window.localStorage.setItem("lat",JSON.stringify(response.data.data.lat));
                window.localStorage.setItem("lng",JSON.stringify(response.data.data.lng));
                this.$router.push({
                  path: "/"+this.$route.query.about_coach,
                  query:{
                    coachid: this.$route.query.coachid
                  }
                })
              }else {
                this.$router.push('/select_address')
              }
						}
						
						

					}).catch(function(error) {
						console.log(error);
					});
				} 

			
		}
	}
</script>

<style scoped>
	@import "../../assets/css/new_address.css";
</style>
